/*
  页面加载完毕后使用AJAX来查询店铺的信息,并填入对应的文本框中
 */
$(function () {
    //获得地址栏输入的参数
    var shopId = getQueryString('shopId');
    var isEdit = shopId ? true : false;
    var formInitial="/o2o/shop/formInitial";
    var shopInfo="/o2o/shop/shopInfo?shopId="+shopId;
    var shopRegister="/o2o/shop/registerShop";
    // 若地址栏携带参数，点击提交就表示为编辑，否则表示为注册
    if (isEdit) {
        shopRegister = '/o2o/shop/modifyShop';
    }
    //若地址栏携带参数，初始化页面时查出对应参数的用户信息，否则显示默认信息
    if(isEdit){
         resultForm();
    }else{initForm()}

    //异步加载获得服务器的数据，生成商铺分类和所属区域的下拉列表(默认)
    function initForm() {
        $.getJSON(formInitial,function (data) {
            if (data !=null){
                var tempHtml='';
                var tempAreaHtml='';
                //将分类信息集合获取遍历生成下拉列表
                data.shopCategoryList.map(function (item, index) {
                    tempHtml+='<option data-id="'+item.shopCategoryId+'">'+item.shopCategoryName+'</option>'
                });
                //将区域集合获取遍历生成下拉列表
                data.areaList.map(function (item,index) {
                    tempAreaHtml+='<option data-id="+'+item.areaId+'">'+item.areaName+'</option>'
                });
                $("#shop-category").html(tempHtml);
                $('#shop-category').removeAttr('disabled');
                $("#area").html(tempAreaHtml);
            }else{

            }
        })
    }

    function resultForm() {
        $.getJSON(shopInfo,function (data) {
          var shop=data.shop;
          $('#shop-name').val(shop.shopName);
          $('#shop-addr').val(shop.shopAddr);
          $('#phone').val(shop.phone);
          $('#shop-desc').val(shop.shopDesc);

          var tempHtml='';
          //显示所有的区域列表以供选择修改
          data.areaList.map(function (item, index) {
              tempHtml+='<option data-id="'+item.areaId+'">'+item.areaName+'</option>'
          })
            //只显示当前用户之前选择的店铺类别，并且无法修改
            var shopCategory='<option data-id="'+shop.shopCategory.shopCategoryId+'">'
                +shop.shopCategory.shopCategoryName+'</option>'
            $('#shop-category').html(shopCategory);
            $('#shop-category').attr("disabled","disabled");
            $('#area').html(tempHtml);
            $('#area').attr('data-id',shop.areaId);

        })
    }

// 点击表单的提交按钮，将表单的字段值获得转化为JSON
    $('#submit').click(function() {
        var shop = {};

        shop.shopName = $('#shop-name').val();
        shop.shopAddr = $('#shop-addr').val();
        shop.phone = $('#shop-phone').val();
        shop.shopDesc = $('#shop-desc').val();

        shop.shopCategory = {
            shopCategoryId : $('#shop-category').find('option').not(function() {
                return !this.selected;
            }).data('id')
        };
        shop.area = {
            areaId : $('#area').find('option').not(function() {
                return !this.selected;
            }).data('id')
        };

        var shopImg = $("#shop-img")[0].files[0];
        var formData = new FormData();

        formData.append('shopImg', shopImg);
        formData.append('shopStr', JSON.stringify(shop));
        //获取输入的验证码
        var verifyCode=$("#j_captcha").val();
        if(!verifyCode){
            $.toast("请输入验证码");
            return;
        }
        formData.append("verifyCode",verifyCode);
        $.ajax({
            url : shopRegister,
            type : 'POST',
            data : formData,
            // contentType:'application/x-www-form-urlencoded',
            processData: false,
            contentType:false,
            success : function(data) {
                if(data.msg){
                    $.toast(data.msg);
                    return;
                }
                if(data.stateCode>=0){
                    $.toast("添加成功，审核中");
                }else{$.toast("添加失败，请检查输入参数");}
            }
        });

        /*    var shopName = $('#shop-name').val();
            var shopAddr = $('#shop-addr').val();
            var phone = $('#shop-phone').val();
            var shopDesc = $('#shop-desc').val();

            var shopCategory = {
                shopCategoryId : $('#shop-category').find('option').not(function() {
                    return !this.selected;
                }).data('id')
            };
            var area = {
                areaId : $('#area').find('option').not(function() {
                    return !this.selected;
                }).data('id')
            };

            var formData = new FormData();
            var shopImg = $("#shop-img")[0].files[0];
            formData.append("shopName",shopName);
            formData.append("shopAddr",shopAddr);
            formData.append("phone",phone);
            formData.append("shopDesc",shopDesc);
            formData.append("shopCategory.shopCategoryId",shopCategory.shopCategoryId);
            formData.append("area.areaId",area.areaId);
            formData.append("imgFile",shopImg);
             $.ajax({
                 url: '/o2o/test',
                 type: 'POST',
                 data:formData,
                 contentType:false,
                 processData:false,
             })

*/

    }
    );





})
/*
查询商铺分类和所属区域，生成列表
 */


/*
  使用AJAX技术来提交表单，以json数据格式提交给服务器
 */